package com.chenjun.mysite.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * ID是Long类型的BaseService
 * 
 * @author Administrator
 *
 * @param <T>
 * @param <M>
 */
public abstract class AbstractBaseService<T extends IdEntity, M extends IBaseMapper<T, Long, M>>
{
	private Class<T> tClazz;
	private Class<M> mClazz;

	/**
	 * 用反射API获取泛型参数的实际类型
	 */
	public AbstractBaseService()
	{
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		tClazz = (Class<T>) params[0];
		mClazz = (Class<M>) params[1];
	}

	public Integer insert(T entity)
	{
		Integer ChangeRowNumber = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			ChangeRowNumber = mapper.insert(entity);
			sqlSession.commit();
		}
		catch (Exception e)
		{
			sqlSession.rollback();
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return ChangeRowNumber;
	}

	public Integer insertBatch(List<T> entityList)
	{
		Integer ChangeRowNumber = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			ChangeRowNumber = mapper.insertBatch(entityList);
			sqlSession.commit();
		}
		catch (Exception e)
		{
			sqlSession.rollback();
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return ChangeRowNumber;
	}
	
	public Integer deleteById(Long id)
	{
		Integer ChangeRowNumber = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			ChangeRowNumber = mapper.deleteById(id);
			sqlSession.commit();
		}
		catch (Exception e)
		{
			sqlSession.rollback();
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return ChangeRowNumber;
	}
	
	
	public Integer deleteBatchIds(List<Long> idList)
	{
		Integer ChangeRowNumber = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			ChangeRowNumber = mapper.deleteBatchIds(idList);
			sqlSession.commit();
		}
		catch (Exception e)
		{
			sqlSession.rollback();
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return ChangeRowNumber;
	}
	
	public Integer update(T entity)
	{
		Integer ChangeRowNumber = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			ChangeRowNumber = mapper.update(entity);
			sqlSession.commit();
		}
		catch (Exception e)
		{
			sqlSession.rollback();
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return ChangeRowNumber;
	}
	
	

	public T selectOne(T entity)
	{
		T t = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			t = (T) mapper.selectById(entity.getId());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return t;
	}

	public T selectById(Long id)
	{
		T t = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			t = (T) mapper.selectById(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return t;
	}

	public List<T> selectList(T entity)
	{
		List<T> list = new ArrayList<T>(0);
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			list = mapper.selectList(entity);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return list;
	}
	
	public List<T> selectAll(Long limitRow)
	{
		List<T> list = new ArrayList<T>(0);
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			list = mapper.selectAll(limitRow);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return list;
	}
	
	public Page<T> selectPageList(T entity,Page<T> page)
	{
		List<T> list = new ArrayList<T>(0);
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			list = mapper.selectPageList(entity,page);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		page.setDatas(list);
		return page;
	}
	
	List<T> selectBatchIds(List<Long> idList)
	{
		List<T> list = new ArrayList<T>(0);
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = (M) sqlSession.getMapper(mClazz);
			list = mapper.selectBatchIds(idList);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return list;
	}
	
	public Integer selectCount(T entity)
	{
		Integer count = null;
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getInstance();
		SqlSession sqlSession = null;
		try
		{
			sqlSession = sqlSessionFactory.openSession();
			M mapper = sqlSession.getMapper(mClazz);
			count = mapper.selectCount(entity);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sqlSession.close();
		}
		return count;
	}

}
